package com.ruoyi.project.system.creditor.controller;

import java.util.List;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.ruoyi.framework.aspectj.lang.annotation.Log;
import com.ruoyi.framework.aspectj.lang.enums.BusinessType;
import com.ruoyi.project.system.creditor.domain.Creditor;
import com.ruoyi.project.system.creditor.service.ICreditorService;
import com.ruoyi.framework.web.controller.BaseController;
import com.ruoyi.framework.web.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.framework.web.page.TableDataInfo;

/**
 * 债权人Controller
 * 
 * @author xn
 * @date 2024-10-06
 */
@Controller
@RequestMapping("/system/creditor")
public class CreditorController extends BaseController
{
    private String prefix = "system/creditor";

    @Autowired
    private ICreditorService creditorService;

    @RequiresPermissions("system:creditor:view")
    @GetMapping()
    public String creditor()
    {
        return prefix + "/creditor";
    }

    /**
     * 查询债权人列表
     */
    @RequiresPermissions("system:creditor:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(Creditor creditor)
    {
        startPage();
        List<Creditor> list = creditorService.selectCreditorList(creditor);
        return getDataTable(list);
    }

    /**
     * 导出债权人列表
     */
    @RequiresPermissions("system:creditor:export")
    @Log(title = "债权人", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(Creditor creditor)
    {
        List<Creditor> list = creditorService.selectCreditorList(creditor);
        ExcelUtil<Creditor> util = new ExcelUtil<Creditor>(Creditor.class);
        return util.exportExcel(list, "债权人数据");
    }

    /**
     * 新增债权人
     */
    @GetMapping("/add")
    public String add()
    {
        return prefix + "/add";
    }

    /**
     * 新增保存债权人
     */
    @RequiresPermissions("system:creditor:add")
    @Log(title = "债权人", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(Creditor creditor)
    {
        return toAjax(creditorService.insertCreditor(creditor));
    }

    /**
     * 修改债权人
     */
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") Long id, ModelMap mmap)
    {
        Creditor creditor = creditorService.selectCreditorById(id);
        mmap.put("creditor", creditor);
        return prefix + "/edit";
    }

    /**
     * 修改保存债权人
     */
    @RequiresPermissions("system:creditor:edit")
    @Log(title = "债权人", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(Creditor creditor)
    {
        return toAjax(creditorService.updateCreditor(creditor));
    }

    /**
     * 删除债权人
     */
    @RequiresPermissions("system:creditor:remove")
    @Log(title = "债权人", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(creditorService.deleteCreditorByIds(ids));
    }
}
